//
//  CAIBehaviorMove.cpp
//  beast_tamer
//
//  Created by  bk on 13/1/22.
//
//

#include "CAIBehaviorMove.h"
#include "CPlayerControl.h"
#include "AnimationDef.h"


CAIBehaviorMove::CAIBehaviorMove():CAIBehavior(),CProcessFunctions(this)
{
    m_nCurrProcess = INVALID;
}

CAIBehaviorMove::~CAIBehaviorMove()
{
    
}

bool CAIBehaviorMove::Init(CAIManager* ptr)
{
    if (ptr)
    {
        m_AiManager = ptr;
        
        Register(Move,&CAIBehaviorMove::onProcessMove);
        return true;
    }
    
    return false;
}

void CAIBehaviorMove::Entering(IStateInput* input)
{
    targetPoint  = ((CMoveCommand*)input)->m_target;
    
    if (MyMaster()->GetPoint().CheckDistance(targetPoint, MonsterDistance))
    {
        m_ShouldExit = true;
    }
    else
    {
        m_nCurrProcess = Move;
        
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
    }
}


void CAIBehaviorMove::Update(IStateInput*)
{
    this->processFun(m_nCurrProcess);
}

void CAIBehaviorMove::Leaving(IStateInput*)
{
    m_ShouldExit = false;
}

int CAIBehaviorMove::ShouldChange(IStateInput*)
{
    if (m_ShouldExit)
    {
        return BATTLE_OBJECT_STATE_WAIT_COMMAND;
    }
    
    return INVALID;
}

void CAIBehaviorMove::onProcessMove()
{
    if (MyMaster()->GetCurrActiveAnimation() != ACTIVE_ANIMATION_ID_MOVE)
    {
        return;
    }
    
    if(MyMaster()->GetPoint().Step(targetPoint, targetPoint, MonsterDistance))
    {
        m_ShouldExit = true;
    }
}
